package com.gxa.clientarticle.service.impl;

/*
 *
 *@author leijun
 *
 *@creating 2020/11/12 9:30
 *
 */

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxa.clientarticle.mapper.AdvertMapper;
import com.gxa.clientarticle.pojo.Advert;
import com.gxa.clientarticle.service.AdvertService;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName AdvertServiceImpl
 * @Description TODO
 * @Author 俊
 * @Date 2020/11/12 9:30
 * @Version 1.0
 */
@Service("AdvertService")
public class AdvertServiceImpl implements AdvertService {

    @Resource
    AdvertMapper advertMapper;

    @Override
    public Integer add(Advert advert) {
        return advertMapper.insertSelective(advert);
    }

    @Override
    public Integer updateState(Integer id, Boolean state) {
        Advert advert = new Advert();
        advert.setState(state);
        Example example = new Example(advert.getClass());
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id",id);
        return advertMapper.updateByExampleSelective(advert,example);
    }

    @Override
    public Integer update(Advert advert) {
        Example example = new Example(advert.getClass());
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id",advert.getId());
        return advertMapper.updateByExampleSelective(advert,example);
    }

    @Override
    public Map list(Integer page, Integer limit) {
        HashMap<String, Object> map = new HashMap<>();
        PageHelper.startPage(page,limit);
        List<Advert> adverts = advertMapper.selectAll();
        PageInfo<Advert> info = new PageInfo<>(adverts);
        map.put("list",info.getList());
        map.put("counts",info.getTotal());
        return map;
    }

    @Override
    public Integer delete(Integer id) {
        return advertMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Map findAll() {
        HashMap<String, Object> map = new HashMap<>();
        List<Advert> adverts = advertMapper.selectAll();
        map.put("list",adverts);
        return map;
    }


}
